//
//  YYEEmoticonToolbar.swift
//  weibo14
//
//  Created by mac on 16/3/13.
//  Copyright © 2016年 mac. All rights reserved.
//
/// 表情类型
///
/// - Recent:  最近
/// - Default: 默认
/// - Emoji:   Emoji description
/// - Lxh:     浪小花
enum YYEEmotionType: Int {
    case Recent = 0, Default, Emoji, Lxh
}
protocol YYEEmoticonToolbarDelegate:NSObjectProtocol {

    // 代理通知谁点了我
    func emoticonToolar(toolBar: YYEEmoticonToolbar, type emotionTypeSelected: YYEEmotionType)
}


import UIKit

class YYEEmoticonToolbar: UIStackView {

    
    var delegate:YYEEmoticonToolbarDelegate?
    
    // 上一次选中的按钮
    var lastSelectedButton: UIButton?

    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        
        // 设置每个按钮大小相等
        distribution = .FillEqually
        
        self.tag = 8888
        
        addButtom("最近", type: .Recent,bgImageName: "compose_emotion_table_left")
        addButtom("默认", type: .Default,bgImageName: "compose_emotion_table_mid")
        addButtom("Emoji", type: .Emoji,bgImageName: "compose_emotion_table_mid")
        addButtom("浪小花", type: .Lxh,bgImageName: "compose_emotion_table_right")
    
    }
    
    private func addButtom(title: String,type:YYEEmotionType,bgImageName:String) ->UIButton {
        
        let btn = UIButton()
        
        // btn.titleLabel?.text = title
        
        btn.setTitle(title, forState: UIControlState.Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        btn.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Selected)
        
        btn.tag = type.rawValue
        
        btn.addTarget(self, action: "btnClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        // 设置背景图
        btn.setBackgroundImage(UIImage(named: "\(bgImageName)_normal"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "\(bgImageName)_selected"), forState: UIControlState.Selected)
        
        self.addArrangedSubview(btn)
        
        return btn
    }
    
    @objc private func btnClick(button:UIButton) {
        
        // btnClickCallBack?(btn:button)
        
        
        lastSelectedButton?.selected = false
        
        button.selected = true

        delegate?.emoticonToolar(self, type: YYEEmotionType(rawValue: button.tag)!)
        
        lastSelectedButton = button
        
    }
    
    
    func selectedButtonWithSection(section:Int) {
    
        // 1. 取到section对应的按钮
        let button = viewWithTag(section)! as! UIButton
        
        if button == lastSelectedButton {
            return
        }
        
        // 2. 选中该按钮
        button.selected = true
        // 3. 让之前的按钮取消选中
        lastSelectedButton?.selected = false
        // 4. 记录当前选中的按钮,供下次使用
        lastSelectedButton = button

    }

    
    
    
    
    
    
    
    
    
    
    
}











